let mongoose = require('mongoose');
const Common = require('../../util/common')

let schema = new mongoose.Schema({
    type : Number, // 0是单日内的时间
    timeId :{
        type:String,
        index:true
    },
    newUser : Number,
    activeUser : Number,
    channel : String,
    onLineTime : Number,
    secondRemain : Number,
    seventhRemain : Number, //七日留存
    monthRemain : Number,
    events : [require('./statics.event')]
},{versionKey:false});


schema.statics = {
    async findOrCreate(_id,time,obj){

        let leftDate = new Date();
        leftDate.setHours(0);
        leftDate.setMinutes(0)
        leftDate.setSeconds(0)
        let user = await this.findOne({userId:_id});
        if(user){
            obj.channel = user.channel
            if(user.lastLoginTime > leftDate.getTime()) { //今天登陆过的
                return user
            }
        }

        let o = {
            userId:_id,
            register:leftDate.getTime(),
            lastLoginTime : Date.now()
        }
        o = Object.assign(o,obj)
        return await this.create(o)
    }
}

schema.methods = {
}

module.exports = schema;